package com.ggggeeennn.exam.service.impl;

import com.ggggeeennn.common.utils.PageUtils;
import com.ggggeeennn.exam.domain.ExamManage;
import com.ggggeeennn.exam.domain.PaperParam;
import com.ggggeeennn.exam.mapper.ExamManageMapper;
import com.ggggeeennn.exam.service.IExamManageService;
import com.ggggeeennn.question.domain.Question;
import com.ggggeeennn.question.mapper.QuestionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 考试管理Service业务层处理
 * 
 * @author ggggeeennn
 * @date 2025-06-30
 */
@Service
public class ExamManageServiceImpl implements IExamManageService 
{
    @Autowired
    private ExamManageMapper examManageMapper;
    @Autowired
    private QuestionMapper questionMapper;

    /**
     * 查询考试管理
     * 
     * @param examCode 考试管理主键
     * @return 考试管理
     */
    @Override
    public ExamManage selectExamManageByExamCode(Long examCode)
    {
        return examManageMapper.selectExamManageByExamCode(examCode);
    }

    /**
     * 查询考试管理列表
     * 
     * @param examManage 考试管理
     * @return 考试管理
     */
    @Override
    public List<ExamManage> selectExamManageList(ExamManage examManage)
    {
        return examManageMapper.selectExamManageList(examManage);
    }

    /**
     * 新增考试管理
     * 
     * @param examManage 考试管理
     * @return 结果
     */
    @Override
    public int insertExamManage(ExamManage examManage)
    {
        //给数据库的json数据类型字段添加默认值
         List<Integer> paperDetail=new ArrayList<>();
        /** 试卷题目ID数组 */
         List<List<Integer>> paperProblemArray=new ArrayList<>();
         paperProblemArray.add(new ArrayList<>());
         paperProblemArray.add(new ArrayList<>());
         paperProblemArray.add(new ArrayList<>());
         examManage.setPaperDetail(paperDetail);
         examManage.setPaperProblemArray(paperProblemArray);
         examManage.setTotalScore(0L);
        return examManageMapper.insertExamManage(examManage);
    }

    /**
     * 修改考试管理
     * 
     * @param examManage 考试管理
     * @return 结果
     */
    @Override
    public int updateExamManage(ExamManage examManage)
    {
        return examManageMapper.updateExamManage(examManage);
    }

    /**
     * 批量删除考试管理
     * 
     * @param examCodes 需要删除的考试管理主键
     * @return 结果
     */
    @Override
    public int deleteExamManageByExamCodes(Long[] examCodes)
    {
        return examManageMapper.deleteExamManageByExamCodes(examCodes);
    }

    /**
     * 删除考试管理信息
     * 
     * @param examCode 考试管理主键
     * @return 结果
     */


    @Override
    public int releaseExam(Integer isPublic,Long examCode) {
        return examManageMapper.releaseExam(isPublic,examCode);
    }

    //查看试卷试题列表
    @Override
    public List<Question> selectPaperList(PaperParam paperParam) {
        //先拿到考试信息
        ExamManage examManage= examManageMapper.selectExamManageByExamCode(paperParam.getExamCode());
        //再拿到试卷试题列表
        List<List<Integer>> paperProblemArray = examManage.getPaperProblemArray();
        //如果需要分页查询
        if(paperParam.getPageNum()!=null&&paperParam.getPageSize()!=null){
            Integer pageNum=paperParam.getPageNum();
            Integer pageSize=paperParam.getPageSize();
        //如果筛选条件有题目类型
        if(paperParam.getQuestionType()!=null){
            Integer questionType = Integer.parseInt(paperParam.getQuestionType());
            if(questionType==1){
                PageUtils.startPage(pageNum, pageSize);
                return questionMapper.selectJudgeQuestionListByIds(paperProblemArray.get(0), paperParam);
            }else if(questionType==2){
                PageUtils.startPage(pageNum, pageSize);
                return questionMapper.selectMultiQuestionListByIds(paperProblemArray.get(1), paperParam);
            }else if(questionType==3){
                PageUtils.startPage(pageNum, pageSize);
                return questionMapper.selectFillQuestionListByIds(paperProblemArray.get(2), paperParam);

            }
        }
        //开启分页查询
        PageUtils.startPage(pageNum, pageSize);
        List<Question> questionList=questionMapper.selectCombinedQuestions(
                            paperProblemArray.get(0),
                            paperProblemArray.get(1),
                            paperProblemArray.get(2),
                            paperParam);
            return questionList;
        }else if(paperParam.getQuestionType()==null){
            //代表是获取试卷预览，不需要分页
            List<Question> questionList=new ArrayList<>();
            List<Question> judgeList=questionMapper.selectJudgeQuestionListByIds(paperProblemArray.get(0), paperParam);
            List<Question> multiList=questionMapper.selectMultiQuestionListByIds(paperProblemArray.get(1), paperParam);
            List<Question> fillList=questionMapper.selectFillQuestionListByIds(paperProblemArray.get(2), paperParam);

//            //编号
//            // 为选择题添加序号 (1. 2. 3. ...)
//            int index = 1;
//            for (Question question : multiList) {
//                question.setQuestion(index + ". " + question.getQuestion());
//                index++;
//            }
//
//            // 为判断题添加序号 (续接选择题的序号)
//            for (Question question : judgeList) {
//                question.setQuestion(index + ". " + question.getQuestion());
//                index++;
//            }
//
//            // 为填空题添加序号 (续接判断题的序号)
//            for (Question question : fillList) {
//                question.setQuestion(index + ". " + question.getQuestion());
//                index++;
//            }


            questionList.addAll(judgeList);
            questionList.addAll(multiList);
            questionList.addAll(fillList);
            
            return questionList;
        }
        List<Question> questionList=questionMapper.selectCombinedQuestions(
                paperProblemArray.get(0),
                paperProblemArray.get(1),
                paperProblemArray.get(2),
                paperParam);
        return questionList;
    }


    @Override
    public int addQuestionstoPaper(PaperParam paperParam) {
        //需要添加的三个id数组
        List<Integer> addJudgeQuestionIds = paperParam.getJudgeQuestionIds();
        List<Integer> addMultiQuestionIds = paperParam.getMultiQuestionIds();
        List<Integer> addFillQuestionIds = paperParam.getFillQuestionIds();


        //通过examCode查出考试信息
        ExamManage examManage = examManageMapper.selectExamManageByExamCode(paperParam.getExamCode());

        //把需要添加的三个id数组分别添加到paperProblemArray中对应数组中
        List<List<Integer>> paperProblemArray = examManage.getPaperProblemArray();
        paperProblemArray.get(0).addAll(addJudgeQuestionIds);
        paperProblemArray.get(1).addAll(addMultiQuestionIds);
        paperProblemArray.get(2).addAll(addFillQuestionIds);

        //更新paper_detail字段
        List<Integer> newPaperDetail = new ArrayList<>();
        newPaperDetail.add(paperProblemArray.get(0).size());
        newPaperDetail.add(paperProblemArray.get(1).size());
        newPaperDetail.add(paperProblemArray.get(2).size());

        //更新totalScore总分字段
        //获取试题列表
        // 计算新增题目的总分
        Long addTotalScore = 0L;

        // 分别查询并计算各题型分数
        if (!addJudgeQuestionIds.isEmpty()) {
            List<Question> judgeList = questionMapper.selectJudgeQuestionListByIds(addJudgeQuestionIds, paperParam);
            for(Question question : judgeList) {
                addTotalScore += question.getScore();
            }
        }

        if (!addMultiQuestionIds.isEmpty()) {
            List<Question> multiList = questionMapper.selectMultiQuestionListByIds(addMultiQuestionIds, paperParam);
            for(Question question : multiList) {
                addTotalScore += question.getScore();
            }
        }

        if (!addFillQuestionIds.isEmpty()) {
            List<Question> fillList = questionMapper.selectFillQuestionListByIds(addFillQuestionIds, paperParam);
            for(Question question : fillList) {
                addTotalScore += question.getScore();
            }
        }

            ExamManage newExamManage = new ExamManage();
            newExamManage.setPaperDetail(newPaperDetail);
            newExamManage.setPaperProblemArray(paperProblemArray);
            newExamManage.setExamCode(paperParam.getExamCode());
            newExamManage.setTotalScore(examManage.getTotalScore() + addTotalScore);
            return examManageMapper.updateExamManage(newExamManage);
        }


        @Override
        public int removePaperQuestion (PaperParam paperParam){

            List<Integer> removeJudgeQuestionIds = paperParam.getJudgeQuestionIds();
            List<Integer> removeMultiQuestionIds = paperParam.getMultiQuestionIds();
            List<Integer> removeFillQuestionIds = paperParam.getFillQuestionIds();

            ExamManage examManage = examManageMapper.selectExamManageByExamCode(paperParam.getExamCode());
            List<List<Integer>> paperProblemArray = examManage.getPaperProblemArray();
            for (Integer id : removeJudgeQuestionIds) {
                paperProblemArray.get(0).remove(id);
            }
            for (Integer id : removeMultiQuestionIds) {
                paperProblemArray.get(1).remove(id);
            }
            for (Integer id : removeFillQuestionIds) {
                paperProblemArray.get(2).remove(id);
            }

            List<Integer> newPaperDetail = new ArrayList<>();
            newPaperDetail.add(paperProblemArray.get(0).size());
            newPaperDetail.add(paperProblemArray.get(1).size());
            newPaperDetail.add(paperProblemArray.get(2).size());


            // 计算要删除题目的总分
            Long removeTotalScore = 0L;

            // 分别查询并计算各题型分数
            if (!removeJudgeQuestionIds.isEmpty()) {
                List<Question> judgeList = questionMapper.selectJudgeQuestionListByIds(removeJudgeQuestionIds, paperParam);
                for(Question question : judgeList) {
                    removeTotalScore += question.getScore();
                }
            }

            if (!removeMultiQuestionIds.isEmpty()) {
                List<Question> multiList = questionMapper.selectMultiQuestionListByIds(removeMultiQuestionIds, paperParam);
                for(Question question : multiList) {
                    removeTotalScore += question.getScore();
                }
            }

            if (!removeFillQuestionIds.isEmpty()) {
                List<Question> fillList = questionMapper.selectFillQuestionListByIds(removeFillQuestionIds, paperParam);
                for(Question question : fillList) {
                    removeTotalScore += question.getScore();
                }
            }
            ExamManage newExamManage = new ExamManage();
            newExamManage.setPaperDetail(newPaperDetail);
            newExamManage.setPaperProblemArray(paperProblemArray);
            newExamManage.setExamCode(paperParam.getExamCode());
            newExamManage.setTotalScore(examManage.getTotalScore() - removeTotalScore);
            return examManageMapper.updateExamManage(newExamManage);
        }

}
